package game;

import java.awt.Color;
import javax.swing.JOptionPane;
import boundaryToMatador.GUI;

public class Game {

	private Player[] players;
	private Player activePlayer; 
	private int numOfPlayers;

	public Game() {

	}
	
	// Creates players and saves them to players array ...
	public void createPlayers(int numOfPlayers)
	{		
		// Creates players
		players = new Player[numOfPlayers];
		
		for(int i = 0 ; i < numOfPlayers; i++){
			players[i] = new Player("Player " + (i + 1 ));
			System.out.println("Opretter spiller " + (i + 1));
		}
		
		activePlayer = players[0];
	}
	
	public void startGame()
	{
		// Dialog to choice amount of players
		Object[] possibleChoices = {"2", "3", "4", "5", "6"};
		String dialogResult = null;
		while (dialogResult == null)
		{
			dialogResult = (String)JOptionPane.showInputDialog(null, "Vælg antal spillere:\n\n", "Antal spillere ...", JOptionPane.QUESTION_MESSAGE, null, possibleChoices, "2");
		}
		numOfPlayers = Integer.parseInt(dialogResult);
		
		// Calls createPlayers() method with number of players selected in dialog
		createPlayers(numOfPlayers);
				
		// Creates GUI
				
	}

	public static void main(String[] args) {

		Game thisGame = new Game();
		
		// Starts the game with gui
		thisGame.startGame();
		thisGame.createGUI();
	}
	
	// playTurn throws the dice and moves to the position the dice shows.
	public void playTurn() {

		DiceCup.rollDice();

		// Updates dice in Gui
		GUI.setDice(DiceCup.getDie1(), 178, DiceCup.getDie2(), 340);
		
		// Moves player and updates players balance
		activePlayer.movePlayer(DiceCup.getSum());
		GameBoard.fields[activePlayer.getPosition()-1].landOnField(activePlayer);
		
		if(checkGameConditions()) {
			presentWinner();
			resetGame();
		} else {
			passTurn();
		}
		
		// Calls methods in the gui to show progress made in game
		updateGui();
		
	}
	
	// Presents the winning player to the gui
	public void presentWinner() {
		Player winner = null;
		for (int i = 0; i < players.length; i++)
		{
			GUI.setBalance(players[i].getName(), players[i].getAccount().getPoint());
			if (players[i].getAccount().getPoint() > 0)
			{
				winner = players[i];
			}
		}
		
		GUI.showMessage(winner.getName() + " wins the game!");	
	}

	// Checks who's the activePlayer and passes the turn to the next player still in the game.
	public void passTurn() {

		int playerIndex = 0;
		for (int i=0 ; i < numOfPlayers ; i++){
			if (players[i] == activePlayer){
				playerIndex = i;
			}
		}

		if (playerIndex < numOfPlayers-1)
		{
			activePlayer = players[playerIndex+1];
		}
		else
		{
			activePlayer = players[0];
		}
		
		// if newly selected activePlayer has no more points, pass turn to next player
		if (activePlayer.getAccount().getPoint() <= 0)
		{
			passTurn();
		}
	}

	// Checks wich players is still in the game, if only one player's left, he is the winner
	public boolean checkGameConditions() {

		if (activePlayer.getAccount().getPoint() <= 0) {
			removeFieldsFromPlayer(activePlayer);
		}
		
		int playersLeft = 0;
		for (int i = 0; i < numOfPlayers; i++)
		{
			if (players[i].getAccount().getPoint() > 0)
			{
				playersLeft++;
			}
		}
		
		return playersLeft==1;

	}
	
	// Remove fields from players who has 0 or less points...
	public void removeFieldsFromPlayer(Player player)
	{
		Ownable field;
		for (int i = 0; i < GameBoard.fields.length; i++)
		{
			if (Ownable.class.isInstance(GameBoard.fields[i]))
			{
				field = (Ownable)GameBoard.fields[i];
				if (field.getOwner() == player)
				{
					field.setOwner(null);
				}
			}
		}
	}

	// Updates details about the game to the gui
	public void updateGui() {
	
		for (int i = 0; i<numOfPlayers; i++){
			GUI.removeAllCars(players[i].getName());	
			if (players[i].getAccount().getPoint() > 0)
			{
				GUI.setCar(players[i].getPosition(), players[i].getName());
			}
			GUI.setBalance(players[i].getName(), players[i].getAccount().getPoint());
		}
		
		if(GUI.getUserButtonPressed(activePlayer.getName() +"'s tur", "Roll Dice") != ""){
			playTurn();
		}
		
	}

	// Resets variables so the game can start over
	public void resetGame() {
		for (int i =0 ; i<numOfPlayers; i++){
			players[i] = new Player("Player " + (i + 1));
		}
	}
	
	// Creates an instance of gui 
	public void createGUI()
	{
		GUI.create("fields.txt");
		
		Color[] colorArray = new Color[6];
		
		colorArray[0] = Color.BLACK;
		colorArray[1] = Color.BLUE;
		colorArray[2] = Color.CYAN;
		colorArray[3] = Color.GRAY;
		colorArray[4] = Color.GREEN;
		colorArray[5] = Color.ORANGE;
		
		for (int i = 0 ; i < numOfPlayers; i++){
			GUI.addPlayer(players[i].getName(), 30000, colorArray[i]);
		}
		
		updateGui();
	}
	
	public Player getPlayer(int i){
		return players[i];
	}
	public Player getActivePlayer(){
		return activePlayer;
	}
}